<template>
    <a-modal
        :title="!currRow ? '添加角色' : '编辑角色'"
        :visible="isShowModal"
        width="40%"
        :footer="null"
        dialogClass="custom-modal"
        @cancel="handleCancel"
        :maskClosable="false"
    >
        <a-form-model
            :model="form"
            ref="form"
            :labelCol="{ span: 5 }"
            :wrapperCol="{ span: 19 }"
            :rules="rules"
            :colon="false"
        >
            <a-row :gutter="10">
                <a-col :span="24" class="mb16">
                    <a-form-model-item label="角色名称" prop="roleName">
                        <a-input placeholder="请输入角色名称" v-model.trim="form.roleName" allowClear />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24" class="mb16">
                    <a-form-model-item label="角色描述" prop="description">
                        <a-textarea
                            placeholder="请输入角色描述"
                            v-model.trim="form.description"
                            allowClear
                            :auto-size="{ minRows: 3, maxRows: 5 }"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24" class="mb16">
                    <a-form-model-item label="角色编码" prop="type">
                        <a-input-number
                            :step="1"
                            :precision="0"
                            :min="1"
                            placeholder="请输入角色编号"
                            v-model="form.type"
                        />
                    </a-form-model-item>
                </a-col>
                <!-- <a-col :span="24">
                    <a-form-model-item label="唯一设备登录">
                        <a-checkbox
                            :checked="form.isOnlyLogin"
                            @change="form.isOnlyLogin = !form.isOnlyLogin"
                        ></a-checkbox>
                    </a-form-model-item>
                </a-col> -->
                <a-col :span="24">
                    <a-button type="primary" class="fr" @click="handleOk" :loading="confirmLoading">确定</a-button>
                </a-col>
            </a-row>
        </a-form-model>
    </a-modal>
</template>
<script>
import { rolesApi } from '../../../../common/api/setting'
import { jobList } from '@/common/config'
export default {
    data() {
        return {
            confirmLoading: false,
            form: {
                roleName: '',
                description: '',
                isOnlyLogin: false,
                type: undefined,
            },
            rules: {
                roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
                type: [{ required: true, message: '请输入角色编码', trigger: 'blur' }],
                description: [{ required: false, message: '请输入角色描述', trigger: 'blur' }],
            },
        }
    },
    components: {},
    props: {
        isShowModal: {
            required: true,
            type: Boolean,
        },
        currRow: {
            required: false,
            type: [Object, undefined],
        },
        enterpriseId: {
            required: true,
            type: String,
        },
    },
    watch: {
        isShowModal: {
            immediate: true,
            handler: function (isShowModal) {
                if (isShowModal) {
                    this.beforeShowModal()
                } else {
                    this.$refs.form && this.$refs.form.resetFields()
                }
            },
        },
    },
    beforeDestroy() {
        this.$refs.form && this.$refs.form.resetFields()
    },
    methods: {
        beforeShowModal() {
            if (this.currRow) {
                const { roleName, description, isOnlyLogin, roleCode } = this.currRow
                Object.assign(this.form, {
                    roleName,
                    description,
                    isOnlyLogin,
                    type: roleCode || undefined,
                })
            }
        },
        async save() {
            const apiType = this.currRow ? 'edit' : 'add'
            const { roleName, description, isOnlyLogin, type: roleCode } = this.form
            await rolesApi[apiType]({
                roleName,
                description,
                isOnlyLogin,
                roleId: this.currRow ? this.currRow.id : '',
                enterpriseId: this.enterpriseId,
                roleCode,
            })
        },
        async handleOk() {
            this.confirmLoading = true
            this.$refs.form.validate(async valid => {
                console.log(valid, 'valid')
                if (valid) {
                    this.save()
                        .then(() => {
                            this.$emit('update:isShowModal', false)
                            this.$message.success('操作成功')
                            this.$emit('updateData')
                        })
                        .catch(e => {
                            console.log(e)
                        })
                        .finally(() => {
                            this.confirmLoading = false
                        })
                } else {
                    this.confirmLoading = false
                    return false
                }
            })
        },
        handleCancel() {
            this.$emit('update:isShowModal', false)
        },
    },
}
</script>
<style scoped></style>
